package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.Suppliermsg;
import com.lovo.bean.Tenderinfo;
import com.lovo.dto.SupplierMsgDto;
import com.lovo.mapper.SuppliermsgMapper;
import com.lovo.service.ISuppliermsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.service.ISupplierproductService;
import com.lovo.service.ITenderinfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class SuppliermsgServiceImpl extends ServiceImpl<SuppliermsgMapper, Suppliermsg> implements ISuppliermsgService {

    @Autowired
    ITenderinfoService tenderinfoService;

    @Autowired
    MQSService mqsService;

    @Autowired
    ISupplierproductService supplierproductService;

    @Override
    public IPage<Suppliermsg> findAllMsg(Integer pageNO, Integer supplierId) {
        QueryWrapper<Suppliermsg> qw = new QueryWrapper<>();
        qw.eq("supplierId", supplierId);
        return baseMapper.selectPage(new Page<>(pageNO, 5), qw);
    }

    @Override
    public void addMsg(Integer tenderId, String size, String batch, String address) {
        Tenderinfo tenderinfo = tenderinfoService.getBaseMapper().selectById(tenderId);

        Suppliermsg suppliermsg = new Suppliermsg();
        suppliermsg.setSupplierid(tenderinfo.getFkSupplierid());
        suppliermsg.setProductid(tenderinfo.getIProductId());
        suppliermsg.setSuppliername(tenderinfo.getSuppliername());
        suppliermsg.setProductname(tenderinfo.getIProductname());
        suppliermsg.setMNum(tenderinfo.getINum());
        suppliermsg.setMPrice(tenderinfo.getIPrice());
        suppliermsg.setMSize(size);
        suppliermsg.setMStatus("未发货");
        suppliermsg.setMBatch(batch);
        suppliermsg.setMCode(String.valueOf(System.currentTimeMillis()));
        suppliermsg.setMAddress(address);
        suppliermsg.setMTime(String.valueOf(LocalDate.now()));
        suppliermsg.setMType(tenderinfo.getIType());


        this.save(suppliermsg);

        tenderinfo.setIStatus("已供货");
        tenderinfoService.getBaseMapper().updateById(tenderinfo);


    }


    @Override
    public void updateStatus(Integer id, String status) {
        Suppliermsg suppliermsg = baseMapper.selectById(id);
        suppliermsg.setMStatus(status);

        baseMapper.updateById(suppliermsg);

        SupplierMsgDto supplierMsgDto = new SupplierMsgDto();
        BeanUtils.copyProperties(suppliermsg, supplierMsgDto);

        mqsService.sendMsg(supplierMsgDto);


    }

    @Override
    public void hdUpdate(Integer id, String status) {
        Suppliermsg suppliermsg = baseMapper.selectById(id);
        suppliermsg.setMStatus(status);
        baseMapper.updateById(suppliermsg);
    }


}
